package com.lanou.musicproject.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.SongListViewAdapter;
import com.lanou.musicproject.bean.Song;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 * 搜索歌手信息
 */
public class SeekSongFragment extends Fragment {
    private String url;
    private ListView song_listview;
    private VolleySingleton volleySingleton;
    private List<Song.DataEntity> data;
    private SongListViewAdapter adapter;
    private int _id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.songfragment, null);
        song_listview = (ListView) view.findViewById(R.id.song_listview);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
        return view;
    }

    private void initData() {
        //接收数据替换q_id
         _id = MyConfig.getSharePreInt(getActivity(),"entiey","_id");
        url = "http://api.dongting.com/song/singer/"+ _id+"/songs?uid=866058021842951&f=3040&os=4.4.4&app=ttpod&hid=1135212123029932&imei=866058021842951&alf=203200&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&size=30&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314&page=1&longitude=121.54483&user_id=0&latitude=38.88351&language=zh&imsi=460078098497828&tid=0";


        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                Song song = JSON.parseObject(str, Song.class);
                data = song.getData();
                adapter = new SongListViewAdapter(getActivity(), data);
                song_listview.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }
}
